package it.unina.lab.anm;

import it.unina.lab.anm.models.Fermata;
import it.unina.lab.anm.models.Linea;
import it.unina.lab.anm.ui.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Message;
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;

public class SpeechListener implements RecognitionListener {

	//private SpeechRecognizer sr = null;
	private View bottomBar = null;
	//private ProgressDialog dialog;
	//private Context context = null;
	
	public SpeechListener(View bb) {
		//sr = speech;
		bottomBar = bb;
		//dialog = dd;

		
		// TODO Auto-generated constructor stub
	}
	
	
	
	public void onReadyForSpeech(Bundle params)
    {
			 //mic.setImageResource(R.drawable.micblu);
			 bottomBar.setVisibility(View.VISIBLE);
			 Message msg = Message.obtain(InteractionManager.getInteractionManagerIstance().getInboxHandler(), Costants.START_USER_INTERACTION);
         	 msg.sendToTarget();
             Log.d("Multimodal", "onReadyForSpeech");
    }
    
	public void onBeginningOfSpeech()
    {
             Log.d("Multimodal", "onBeginningOfSpeech");
            
    }
    
    public void onRmsChanged(float rmsdB)
    {
             //Log.d("Multimodal", "onRmsChanged - value: " + rmsdB);
    }
    
    public void onBufferReceived(byte[] buffer)
    {
             //Log.d("Multimodal", "onBufferReceived");
    }
    
    public void onEndOfSpeech()
    {	
    		 //mic.setImageResource(R.drawable.micgrigio);
    		 bottomBar.setVisibility(View.INVISIBLE);
             Log.d("Multimodal", "onEndofSpeech");
    }
    
    public void onError(int error)
    {
    		if(error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)
    			Log.e("Multimodal",  "ERROR:  Speech Timeout...RESTART new Frame!");
    		else
    			Log.i("Multimodal",  "ERROR: " + error);
    		
    		 bottomBar.setVisibility(View.INVISIBLE);
    			
        
    }
    
    public void onResults(Bundle results){
             Log.d("Multimodal", "onResults " + results);
             ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
             for (int i = 0; i < data.size(); i++)
             {
            			String dettata = (String) data.get(i);
            			Log.d("Multimodal", "frase " + dettata);	
                       //Log.d("Multimodal", "result " + data.get(i));
                       //sendEvent(new ModalityEvent("speech", "task", (String)data.get(i)));
             }
            
            HashMap<String, Object> semanticResult = UIUtils.searchwords((String) data.get(0));
            if(semanticResult.containsKey("linea")){
            	Linea ln = new Linea((String)semanticResult.get("linea"));
            	Message msg = Message.obtain(InteractionManager.getInteractionManagerIstance().getInboxHandler(), Costants.EVENT_MESSAGE, ln);
            	msg.sendToTarget();
            }
            
            if(semanticResult.containsKey("fermata") && ((Boolean)semanticResult.get("fermata"))){
            	Fermata fr = new Fermata(null, 0, 0, null, Fermata.ALL_FERMATE);
            	Message msg = Message.obtain(InteractionManager.getInteractionManagerIstance().getInboxHandler(), Costants.EVENT_MESSAGE, fr);
            	msg.sendToTarget();
            }
            
            if(semanticResult.containsKey("linee") && ((Boolean)semanticResult.get("linee"))){
            	Linea ln = new Linea(Linea.ALL_LINEE);
            	Message msg = Message.obtain(InteractionManager.getInteractionManagerIstance().getInboxHandler(), Costants.EVENT_MESSAGE, ln);
            	msg.sendToTarget();
            }
            
            
            Message msg = Message.obtain(InteractionManager.getInteractionManagerIstance().getInboxHandler(), Costants.END_USER_INTERACTION);
        	msg.sendToTarget();
     		
             //String frase = (String) data.get(0);
             //Log.d("Multimodal", "result " + frase);
             //String [] tokens = frase.split(" ");
             //Log.d("Multimodal", "tokens " + tokens);
             
         /*  try{  
             InputStream is = context.getResources().openRawResource (R.raw.test);
             SrgsXmlDocument xml_gram = new SrgsXmlDocument(new InputSource(is));
 			 SrgsXmlGrammarParser parser = new SrgsXmlGrammarParser();
 			 GrammarGraph grafo = parser.parse(xml_gram);
 			 GrammarChecker checker = new GrammarChecker(grafo);
 			 boolean isValido = checker.isValid(tokens);
 			 if(isValido){
 				Log.d("Multimodal","frase valida");
 				for(int i=0; i<checker.getInterpretation().length; i++)
 					Log.d("Multimodal",checker.getInterpretation()[i]);
 			}	
           }
           catch (Exception e) {
			Log.d("Multimodal", "Eccezione");
		} */
 			 
 		
             
             //Log.i("Multimodal",  "RESTART LISTENING!!");
             //sr.startListening(intent);
                
    }



	@Override
	public void onEvent(int eventType, Bundle params) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void onPartialResults(Bundle partialResults) {
		Log.d("Multimodal", "onPartialResults");
		
	}
	

	
}
